{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8f8b42b1",
   "metadata": {
    "lines_to_next_cell": 2
   },
   "source": [
    "# LangGraph 101: Conditional Routing\n",
    "\n",
    "## Imports and State Definition\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e96befc2",
   "metadata": {
    "lines_to_next_cell": 1
   },
   "outputs": [],
   "source": [
    "# ~ Imports\n",
    "from typing import TypedDict\n",
    "from langgraph.graph import StateGraph, START, END\n",
    "from IPython.display import Image, display\n",
    "from rich import print"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e9389da",
   "metadata": {},
   "source": [
    "## State Definition\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2bb60dfc",
   "metadata": {},
   "outputs": [],
   "source": [
    "class NumberState(TypedDict):\n",
    "    number: int\n",
    "    result: int"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d60f7ee",
   "metadata": {},
   "source": [
    "## Node Functions and Router\n",
    "\n",
    "We've set up three processing nodes to handle different types of numbers:\n",
    "\n",
    "- **Positive numbers**: We square the number.\n",
    "- **Negative numbers**: We take the absolute value.\n",
    "- **Zero**: We leave it as zero.\n",
    "\n",
    "Additionally, we've built a router function that decides which path to follow based on the sign of the input number.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cb6e0503",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e6bdf33b",
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "439e87a1",
   "metadata": {},
   "source": [
    "## Building the Conditional Routing Graph\n",
    "\n",
    "1.  Start → Router node\n",
    "2.  Router evaluates the number's sign\n",
    "3.  Routes to the appropriate processing node\n",
    "4.  All branches lead to END\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f4a7293",
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "f7fe9611",
   "metadata": {},
   "source": [
    "## Visualizing the Graph\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5d98b82c",
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "a4ae612e",
   "metadata": {},
   "source": [
    "## Testing the Conditional Routing Graph\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "46e5aee8",
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "40b4a4f4",
   "metadata": {},
   "source": [
    "# Example 2: Text Message Processing Router\n",
    "\n",
    "Let's create another conditional routing example using text messages. This will demonstrate how to route based on text length and apply different processing strategies.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80b5e56d",
   "metadata": {},
   "source": [
    "## Text State Definition\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1740897b",
   "metadata": {},
   "outputs": [],
   "source": [
    "class MessageState(TypedDict):\n",
    "    # * test \n",
    "    message: str\n",
    "    processed_message: str\n",
    "    word_count: int"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "670aaa45",
   "metadata": {},
   "source": [
    "## Text Processing Nodes and Router\n",
    "\n",
    "We'll route messages based on their length and apply different processing:\n",
    "\n",
    "- **Short messages** (≤ 10 chars): Convert to uppercase and add exclamation\n",
    "- **Medium messages** (11-50 chars): Add a friendly greeting prefix\n",
    "- **Long messages** (> 50 chars): Summarize by counting words and truncating\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7fa2ffb7",
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "43137008",
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "c4c3abbe",
   "metadata": {},
   "source": [
    "## Building the Text Processing Graph\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5caaf5fb",
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "00e054f6",
   "metadata": {},
   "source": [
    "## Visualizing the Text Processing Graph\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb5b63e9",
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "ebfed763",
   "metadata": {},
   "source": [
    "## Testing the Text Processing Router\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "46dae96c",
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "formats": ".jupytext-sync-ipynb//ipynb,py:percent"
  },
  "kernelspec": {
   "display_name": "py312",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
